package com.hepinghospital.user.ui.activity.eyecheck;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.BaseListResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jz.joyoung.robot.util.DpUtil;

/**
 * 眼科医院列表
 *
 * @time 2021/1/6 15:18
 */
public class EyeHospitalActivity extends BaseActivity {


    List<BaseListResponse> list = new ArrayList<>();
    int current = 1;
    String tital = "", helpCode = "", helpName = "";
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private EyeHospitalAdapter eyeHospitalAdapter;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_eye_hospitai;
    }

    @Override
    protected void initViews() {
        tvTitle.setText("选择检查医院");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvList.setLayoutManager(manager);
        eyeHospitalAdapter = new EyeHospitalAdapter();
        rvList.setAdapter(eyeHospitalAdapter);
        rvList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px(mContext, 10);
            }
        });

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> getEyeHospitalList(helpCode, tital));
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            current++;
            getEyeHospitalList(helpCode, tital);
        });
    }

    @Override
    protected void initData() {
        helpCode = getIntent().getStringExtra("helpCode");
        helpName = getIntent().getStringExtra("helpName");
        current = 1;
        getEyeHospitalList(helpCode, tital);
    }


    /**
     * 帮助小类列表
     *
     * @time 2020/5/11 16:00
     */
    class EyeHospitalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        @NotNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_hospital, parent, false);
            return new ViewHolder(view);

        }

        @Override
        public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, final int position) {
            final ViewHolder myHolder = (ViewHolder) holder;

            myHolder.tvHospitalName.setText(list.get(position).hosName);
            myHolder.tvHospitalAddress.setText(list.get(position).hosAddr);

            myHolder.itemView.setOnClickListener(v -> {
                Intent mIntent = new Intent();
                mIntent.putExtra("eyeHospital", list.get(position));
                setResult(RESULT_OK, mIntent);
                finish();
            });
        }


        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_hospital_name)
            TextView tvHospitalName;

            @BindView(R.id.tv_hospital_address)
            TextView tvHospitalAddress;

            ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

    /**
     * 医院列表
     *
     * @time 2021/1/6 15:22
     */
    private void getEyeHospitalList(String type, String tital) {
        RestProxy.getInstance().getEyeHospitalList("", "", new BaseObserver<List<BaseListResponse>>() {
            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
            }


            @Override
            public void onSuccess(List<BaseListResponse> result) {
                try {
                    Log.e("问题个数---", result + "");
                    list.clear();
                    list.addAll(result);
                    eyeHospitalAdapter.notifyDataSetChanged();
                } catch (Exception e) {
                    // 捕获异常，如果要捕获Json数据异常，写catch(JSONException e)
                    e.printStackTrace();
                    //打印出来就Log.e(TAG,Log.getStackTraceString(e));
                }
                refreshLayout.finishRefresh(true);
            }
        });
    }
}
